#pragma once
// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: web_view_host_window.h
// Description: WebViewHostWindow Class
//      Author: Ziming Li
//     Created: 2020-10-16
// -------------------------------------------------
// Copyright (C) 2020 MingYang Software Technology.
// -------------------------------------------------

#ifndef BLINKIT_WEB_VIEW_HOST_WINDOW_H
#define BLINKIT_WEB_VIEW_HOST_WINDOW_H

#include "blinkit/win/page_popup_session.h"
#include "blinkit/win/web_page_host_window.h"

struct BkWebViewClient;

namespace BlinKit {

class AnimationFrame;

class WebViewHostWindow final : public WebPageHostWindow
{
public:
    static WebViewHostWindow* CreateInstance(HWND hWnd, LPCREATESTRUCT cs);
    ~WebViewHostWindow(void) override;

    void StartPagePopupSession(PagePopupHostWindow *host);
    void StopPagePopupSession(void);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Exports
    bool ProcessMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT &result);
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
    WebViewHostWindow(const BkWebViewClient &client, HWND hWnd, LPCREATESTRUCT cs);

    static bool ForwardMessage(const BkWebViewClient &client, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT &result);

    static float ScaleFactorFromDPI(UINT dpi);

    void OnChar(HWND hwnd, TCHAR ch, int cRepeat);
    BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
    void OnDPIChanged(HWND hwnd, UINT newDPI, const RECT *rc);
    void OnIMEStartComposition(HWND hwnd);
    void OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
    void OnKillFocus(HWND hwnd, HWND hwndNewFocus);
    void OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags);
    int OnMouseActivate(HWND hwnd, HWND hwndTopLevel, UINT codeHitTest, UINT msg);
    void OnMouseWheel(HWND hwnd, int xPos, int yPos, int zDelta, UINT fwKeys);
    bool OnNCCreate(WPARAM wParam, LPARAM lParam, LRESULT &result);
    bool OnNCDestroy(HWND hwnd, WPARAM wParam, LPARAM lParam, LRESULT &result);
    void OnSetFocus(HWND hwnd, HWND hwndOldFocus);
    void OnShowWindow(HWND hwnd, BOOL fShow, UINT status);
    void OnSize(HWND hwnd, UINT state, int cx, int cy);

    void SetResizingTimer(void);
    void KillResizingTimer(void);
    void AdjustUpdateWhileResizing(DWORD tick);
    static void CALLBACK ResizingTimerProc(HWND, UINT, UINT_PTR timerId, DWORD tick);

    // WebPageHost
    bool CanBeClosed(void) const override;
    void Close(void) override;
    void ChangeTitle(const std::string &title) override;
    void ShowContextMenu(const WebContextMenuData &data) override;
    WebScreenInfo GetScreenInfo(void) override;

    std::unique_ptr<WebViewImpl> m_webView;
    PagePopupSession m_pagePopupSession;

    DWORD m_resizingTick = 0;
    UINT_PTR m_resizingTimerId = 0;
    static std::unordered_map<UINT_PTR, WebViewHostWindow *> m_resizingHosts;
};

} // namespace BlinKit

#endif // BLINKIT_WEB_VIEW_HOST_WINDOW_H
